using System;
using System.ComponentModel;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;

namespace OkTool.Util
{
    public class ConvertUtil
    {


        /// <summary>
        /// 转换为Guid
        /// </summary>
        /// <param name="value"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static Guid ToGuid(object value, Guid defaultValue)
        {
            try
            {
                return Guid.Parse(value.ToString());
            }
            catch (Exception)
            {
                return defaultValue;
            }
        }

        /// <summary>
        /// 转换为字符串
        /// <para>转换失败返回默认值,默认值为null</para>
        /// </summary>
        /// <param name="value">被转换的值</param>
        /// <param name="defaultValue">转换错误时的默认值</param>
        /// <returns></returns>
        public static string ToStr(object value, string defaultValue = null)
        {
            try
            {
                return value.ToString();
            }
            catch (Exception)
            {
                return defaultValue;
            }
        }

        /// <summary>
        /// 转换为string数组
        /// <para>转换失败返回null</para>
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string[] ToStrArray(object value)
        {
            try
            {
                return value.ToString().ToCharArray().Select(c => c.ToString()).ToArray();
            }
            catch (Exception)
            {
                return null;
            }
        }

        /// <summary>
        /// 转换为字符
        /// <para>转换失败返回默认值,默认值为null</para>
        /// </summary>
        /// <param name="value"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static char? ToChar(object value, char? defaultValue = null)
        {
            try
            {
                return System.Convert.ToChar(value);
            }
            catch (Exception)
            {
                return defaultValue;
            }
        }

        /// <summary>
        /// 转换为char数组
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static char[] ToCharArray(object value)
        {
            try
            {
                return value.ToString().ToCharArray();
            }
            catch (Exception)
            {
                return null;
            }
        }

        /// <summary>
        /// 转换为byte
        /// <para>如果给定的值为null或者转换失败,返回默认值</para>
        /// </summary>
        /// <param name="value"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static byte? ToByte(object value, byte? defaultValue = null)
        {
            try
            {
                return System.Convert.ToByte(value);
            }
            catch (Exception)
            {
                return null;
            }
        }

        /// <summary>
        /// 转换为byte数组
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static byte[] ToByteArray(object value)
        {
            if (value == null)
                return null;

            Type type = value.GetType();

            if (type == typeof(int))
            {
                return BitConverter.GetBytes((int)value);
            }
            else if (type == typeof(double))
            {
                return BitConverter.GetBytes((double)value);
            }
            else if (type == typeof(string))
            {
                string str = value.ToString();
                return Encoding.UTF8.GetBytes(str);
            }else
            {
                BinaryFormatter bf = new BinaryFormatter();
                using (MemoryStream ms = new MemoryStream())
                {
                    bf.Serialize(ms, value);
                    return ms.ToArray();
                }
            }
        }

        /// <summary>
        /// 转换为short
        /// </summary>
        /// <param name="value"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static short? ToShort(object value, short? defaultValue = null)
        {
            try
            {
                return Int16.Parse(value.ToString());
            }
            catch (Exception)
            {
                return defaultValue;
            }
        }

        /// <summary>
        /// 转为decimal
        /// <para>转换失败返回默认值,默认值为null</para>
        /// </summary>
        /// <param name="value">被转换的值</param>
        /// <param name="defaultValue">转换失败的默认值</param>
        /// <returns>int类型的数字</returns>
        public static decimal? ToDecimal(object value, decimal? defaultValue = null)
        {
            try
            {
                return System.Convert.ToDecimal(value.ToString());
            }
            catch (Exception)
            {
                return defaultValue;
            }
        }

        /// <summary>
        /// 转为int
        /// <para>如果给定的值为null或者转换失败,返回默认值,默认值为null</para>
        /// </summary>
        /// <param name="value">被转换的值</param>
        /// <param name="defaultValue">转换失败的默认值</param>
        /// <returns></returns>
        public static int? ToInt(object value, int? defaultValue = null)
        {
            try
            {
                return System.Convert.ToInt32(value.ToString());
            }
            catch (Exception)
            {
                return defaultValue;
            }
        }

        /// <summary>
        /// 转换为long
        /// <para>如果给定的值为null或者转换失败,返回默认值,默认值为null</para>
        /// </summary>
        /// <param name="value"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static long? ToLong(object value, long? defaultValue = null)
        {
            try
            {
                return System.Convert.ToInt64(value.ToString());
            }
            catch (Exception)
            {
                return defaultValue;
            }
        }
        /// <summary>
        /// 转为bool
        /// <para>转换失败返回默认值,默认值为null</para>
        /// <para>如果为["true", "yes", "y", "t", "ok", "1", "on", "是", "对", "真", "對", "√"]，返回{@code true}</para>
        ///  <para>如果为["false", "no", "n", "f", "0", "off", "否", "错", "假", "錯", "×"]，返回{@code false}</para>
        /// </summary>
        /// <param name="value">被转换的值</param>
        /// <param name="defaultValue">转换失败的默认值</param>
        /// <returns></returns>
        public static bool? ToBool(object value, bool? defaultValue = null)
        {
            try
            {
                return BoolUtil.ToBooleanObject(value.ToString());
                //return System.Convert.ToBoolean(value.ToString());
            }
            catch (Exception)
            {
                return defaultValue;
            }
        }

        /// <summary>
        /// 转为double
        /// <para>转换失败返回默认值,默认值为null</para>
        /// </summary>
        /// <param name="value">被转换的值</param>
        /// <param name="defaultValue">转换失败的默认值</param>
        /// <returns></returns>
        public static double? ToDouble(object value, double? defaultValue = null)
        {
            try
            {
                return System.Convert.ToDouble(value.ToString());
            }
            catch (Exception)
            {
                return defaultValue;
            }
        }

        /// <summary>
        /// 转为DateTime
        /// <para>转换失败返回默认值,默认值为null</para>
        /// </summary>
        /// <param name="value">被转换的值</param>
        /// <param name="defaultValue">转换失败的默认值</param>
        /// <returns></returns>
        public static DateTime? ToDateTime(object value, DateTime? defaultValue = null)
        {
            try
            {
                return System.Convert.ToDateTime(value.ToString());
            }
            catch (Exception)
            {
                return defaultValue;
            }
        }

        /// <summary>
        /// 将枚举值或者枚举属性名称 转换为Enum对象
        /// </summary>
        /// <param name="value">枚举值</param>
        /// <param name="defaultValue"></param>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static T ToEnum<T>(object value, T defaultValue)
        {
            try
            {
                return (T)Enum.Parse(typeof(T), value.ToString(), true);
            }
            catch (Exception)
            {
                return defaultValue;
            }
        }

        /// <summary>
        /// 将stream转换成byte[]
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public static byte[] ToByte(Stream stream)
        {
            byte[] bytes = new byte[stream.Length];
            stream.Seek(0, SeekOrigin.Begin);
            stream.Read(bytes, 0, bytes.Length);
            return bytes;
        }

        /// <summary>
        /// 将byte[]转成Stream
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public static Stream ToStream(byte[] bytes)
        {
            Stream stream = new MemoryStream(bytes);
            return stream;
        }
       
        /// <summary>
        /// Stream转bytes
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public static byte[]ToBytes(Stream stream)
        {
            using (MemoryStream memoryStream = new MemoryStream())
            {
                stream.CopyTo(memoryStream);
                return memoryStream.ToArray();
            }
        }
    }
}